#include "specialability.h"
#include "specialabilitylevel.h"
#include "characterclass.h"

#include <QDebug>

SpecialAbility::SpecialAbility(QObject *parent) :
    ModelItem(parent),
    m_multiple(0),
    m_type(0)
{
    qRegisterMetaType<SpecialAbilityLevels*>();
}


SpecialAbility::SpecialAbility(const SpecialAbility &other) :
    ModelItem(other),
    m_multiple(other.multiple()),
    m_type(other.type())
{
}


int SpecialAbility::multiple() const
{
    return m_multiple;
}


int SpecialAbility::type() const
{
    return m_type;
}


void SpecialAbility::setMultiple(int arg)
{
    if (m_multiple != arg) {
        m_multiple = arg;
        emit multipleChanged(arg);
    }
}


void SpecialAbility::setType(int arg)
{
    if (m_type != arg) {
        m_type = arg;
        emit typeChanged(arg);
    }
}

void SpecialAbility::addCharacterLevel(CharacterClass *characterClass, const int &level)
{
    SpecialAbilityLevels* specialLevels = findChild<SpecialAbilityLevels*>(QString(), Qt::FindDirectChildrenOnly);
    if (!specialLevels)
    {
        specialLevels = new SpecialAbilityLevels(this);
    }

    foreach (SpecialAbilityLevel* specialLevel, specialLevels->findChildren<SpecialAbilityLevel*>(QString(), Qt::FindDirectChildrenOnly))
    {
        if (specialLevel->referenceItem() != characterClass)
        {
            continue;
        }

        if (!multiple())
        {
            qDebug() << Q_FUNC_INFO << "Can't have multiples of" << name();
            return;
        }

        if (qvariant_cast<int>(specialLevel->property("level")) == level)
        {
            qDebug() << Q_FUNC_INFO << characterClass->name() << "already has" << name() << "at level" << level;
            return;
        }
    }

    SpecialAbilityLevel* specialLevel = specialLevels->createItem();
    specialLevel->setReferenceItem(characterClass);
    specialLevel->setProperty("level", level);
}


